﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.BusinessRule;


public partial class System_SystemConfig : PageBase
{

    private static string hidvalue = "采集资产|采集KPI|采集订单|采集库存|扫描条码|扫描RFID|采集GPS|异常情况需要拍照|采集订单需要经销商|需要拍照|配置数据|采集CPR";
    private ConfigManager manager = new ConfigManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindConfig(); ;
            IsBind();
        }
    }

    protected void BindConfig()
    {
        ArrayList conf = new ArrayList();
        string[] titles = hidvalue.Split('|');
        int i = 0;
        foreach (string s in titles)
        {
            conf.Add(new Configs(s, i++));
        }
        DataList1.DataSource = conf;
        DataList1.DataBind();

        i = 0;
        SystemConfig config = manager.GetSystemConfig(ConfigEnum.VISIT_CONFIG, ConfigEnum.SUB_KEY001, MyStateManager.DomainID);
        string[] configValue = config.Value.Split('|');
        foreach (DataListItem dli in DataList1.Items)
        {
            var c = (CheckBox)dli.FindControl("CheckBox1");
            if (i < configValue.Length)
            {
                c.Checked = configValue[i++] == "1";
            }
            else
            {
                break;
            }
        }

        if (configValue.Length>i)
            txt_gpstime.Text = configValue[i++];
        if (configValue.Length > i)
        txt_region.Text = configValue[i++];
        if (configValue.Length > i)
        txt_VisitTime.Text = configValue[i++];
        var dt = new DictionaryItemsManager().GetItemList(DictionaryEnum.PhotoType, MyStateManager.DomainID);
        ddlIntoStore.DataSource = dt;
        ddlIntoStore.DataBind();
        ddlIntoStore.Items.Insert(0,new ListItem("--不选择--", ""));
        if (configValue.Length > i)
        {
            String value = configValue[i++];
            if(!String.IsNullOrEmpty(value))
                ddlIntoStore.SelectedValue = value;
        }
        ddlLeaveStore.DataSource = dt;
        ddlLeaveStore.DataBind();
        ddlLeaveStore.Items.Insert(0, new ListItem("--不选择--", ""));
        if (configValue.Length > i)
        {
            String value = configValue[i++];
            if (!String.IsNullOrEmpty(value))
                ddlLeaveStore.SelectedValue = value;
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        decimal lastupdatetime = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        foreach (DataListItem dli in DataList1.Items)
        {
            CheckBox c = (CheckBox)dli.FindControl("CheckBox1");
            if (c != null)
            {
                sb.Append(c.Checked ? "1" : "0");
                sb.Append("|");
               
            }
        }
        

        if (txt_gpstime.Text != "")
            sb.Append(txt_gpstime.Text.Trim());
        else
             sb.Append(15);
        sb.Append("|");
        if (txt_region.Text.Trim() != "")
             sb.Append(txt_region.Text.Trim());
           
        else
            sb.Append(0);
        sb.Append("|");
        if (txt_VisitTime.Text.Trim() != "")
             sb.Append(txt_VisitTime.Text.Trim());
        else
            sb.Append(0);
        sb.Append("|");
        if(ddlIntoStore.SelectedIndex>0)
        {
             sb.Append(ddlIntoStore.SelectedValue);
        }
        else
        {
             sb.Append("");
        }
        sb.Append("|");
         if(ddlLeaveStore.SelectedIndex>0)
        {
             sb.Append(ddlLeaveStore.SelectedValue);
        }
        else
        {
             sb.Append("");
        }
        var config = manager.GetSystemConfig(ConfigEnum.VISIT_CONFIG, ConfigEnum.SUB_KEY001, MyStateManager.DomainID);
        config.Value = sb.ToString();
        config.LAST_UPDATER = MyStateManager.UserID.ToString();
        config.LAST_UPDATE_TIME = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
        try
        {
            config.Save();
             Response.Write("<script>alert('保存成功')</script>");
        }
        catch
        {
             Response.Write("<script>alert('保存失败')</script>");
        }
       
           
    }

    //初始化手机用户捆绑
    protected void IsBind()
    {
        var config = manager.GetSystemConfig(ConfigEnum.ENABLE_DEVICE_BOUND, ConfigEnum.SUB_KEY001, MyStateManager.DomainID);
        if (config!= null && "1".Equals(config.Value))
            cbIsBind.Checked = true;
    }

    protected void btnSaveBind_Click(object sender, EventArgs e)
    {
        bool value;
        var config = manager.GetSystemConfig(ConfigEnum.ENABLE_DEVICE_BOUND, ConfigEnum.SUB_KEY001, MyStateManager.DomainID);
        try
        {
            config.Value=cbIsBind.Checked ? "1" : "0";
             config.LAST_UPDATER = MyStateManager.UserID.ToString();
        config.LAST_UPDATE_TIME = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
            config.Save();
            Response.Write("<script>alert('保存成功')</script>");
        }
        catch
        {
            Response.Write("<script>alert('保存失败')</script>");
        }
       
       
    }
    class Configs
    {
        public Configs(string name, int list)
        {
            Name = name;
            List = list;
        }
        public String Name
        {
            get;
            set;
        }
        public int List
        {
            get;
            set;
        }
    }

}
